package com.example.library.vPage;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.app.Context;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

import static ohos.multimodalinput.event.TouchEvent.POINT_MOVE;
import static ohos.multimodalinput.event.TouchEvent.PRIMARY_POINT_DOWN;

public class BezierViewPager extends PageSlider implements Component.TouchEventListener {
  private boolean touchable = true;
  private ShadowTransformer cardShadowTransformer;
  private boolean isSlipRight;

  public BezierViewPager(Context context) {
    super(context);
    setTouchEventListener(this::onTouchEvent);
  }

  public BezierViewPager(Context context, AttrSet attrs) {
    super(context, attrs);
    setTouchEventListener(this::onTouchEvent);
  }

  public void setTouchable(boolean isCanScroll) {
    this.touchable = isCanScroll;
  }

  @Override
  public void scrollTo(int x, int y) {
    super.scrollTo(x, y);
  }


  @Override
  public void setCurrentPage(int itemPos, boolean smoothScroll) {
    super.setCurrentPage(itemPos, smoothScroll);
  }

  @Override
  public void setCurrentPage(int itemPos) {
    super.setCurrentPage(itemPos);
  }


//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent arg0) {
//        if (touchable) {
//            return super.onInterceptTouchEvent(arg0);
//        } else {
//            return false;
//        }
//
//    }


  public void showTransformer(float zoomIn) {
    if (CardAdapter.class.isInstance(getProvider())) {
      if (cardShadowTransformer == null) {
        cardShadowTransformer = new ShadowTransformer();
        cardShadowTransformer.attachViewPager(this, (CardAdapter) getProvider());
      }
      cardShadowTransformer.setZoomIn(zoomIn);

    }
  }

  private float startX;
  private float moveX;

  @Override
  public boolean onTouchEvent(Component component, TouchEvent ev) {
    if (touchable) {
      MmiPoint pointerPosition;
      switch (ev.getAction()) {
        case PRIMARY_POINT_DOWN:
          pointerPosition = ev.getPointerPosition(0);
          startX = pointerPosition.getX();
          break;
        case POINT_MOVE:
          pointerPosition = ev.getPointerPosition(0);
          moveX = pointerPosition.getX();
          if (moveX > startX) {
            isSlipRight = true;
          } else {
            isSlipRight = false;
          }
          break;
      }
      return true;
    } else {
      return false;
    }
  }
}
